package com.qf.security.service;

import com.qf.exeptions.QueryDataException;
import com.qf.exeptions.ResultCode;
import com.qf.pojo.BaseUser;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;

/*
杨威
2022/1/7
19:47
*/
@Service
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private UserService userService;
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userService.findUserByUserName(s);
        Assert.notNull(user,()->{
            throw new QueryDataException("用户不存在", ResultCode.DATA_EMPTY);
        });
        ArrayList<GrantedAuthority> list=new ArrayList<>();
        return new BaseUser(user.getUId(),user.getUsername(),user.getPassword(),list);
    }
}
